/*
 * Copyright (C) 2003, Velocityme Partnership
 * IssueSessionBean.java
 *
 * Created on 22 May 2003, 06:53
 */

package com.velocityme.session;

import com.velocityme.entity.ChangeDeltaItemTypeBean;
import com.velocityme.entity.ChangeDeltaTypeBean;
import com.velocityme.entity.PermissionBean;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
import com.velocityme.interfaces.*;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.valueobjects.*;
import java.rmi.RemoteException;

/**
 *
 * @author  Robert Crida Work
 * @ejb.bean
 *           type="Stateless"
 *           cmp-version="2.x"
 *           name="IssueSession"
 *           jndi-name="ejb/IssueSession"
 *           view-type="local"
 *           transaction-type="Container"
 * @ejb.transaction type="Required"
 *
 * @ejb.util generate="physical"
 */
public class IssueSessionBean implements SessionBean {
    
    private SessionContext m_context;
    
    private SearchSessionLocal m_searchSessionLocal;

    private PermissionLocal m_permissionIssueCreate;
    private PermissionLocal m_permissionIssueEdit;
    private PermissionLocal m_permissionNodeView;

    /**
     * If the user has permission to create a new Issue in the specified parent
     * node then go ahead and create it and log an appropriate ChangeDelta.
     *
     * @param p_key User's security key
     * @param p_parentNode The node which will contain the new Issue
     * @param p_issueValue The value details for the Issue part of the issue
     *
     * @ejb.interface-method view-type="local"
     **/
    public IssueLocal createIssue(KeySessionLocal p_keyLocal,
                             NodeLocal p_parentNodeLocal,
                             com.velocityme.valueobjects.IssueValue p_issueValue) 
        throws PermissionDeniedException, InvalidKeyException {
        try {
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, p_parentNodeLocal).contains(m_permissionIssueCreate)) {
                DBObjectLocal dBObjectLocal = DBObjectUtil.getLocalHome().create();
                TaskValue taskValue = p_issueValue.getTaskValue();
                taskValue.setType("Issue");
                taskValue.getNodeValue().setFinalType("Issue");
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.CREATE, p_keyLocal.getUserLocal(), dBObjectLocal);
                TaskLocal taskLocal = TaskSessionUtil.getLocalHome().create().createTaskHelper(p_keyLocal, p_parentNodeLocal, taskValue, changeDeltaLocal);
                IssueLocal issueLocal = IssueUtil.getLocalHome().create(taskLocal, p_issueValue);
                return issueLocal;
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission to moderate the issue then do so and log the
     * changes with an appropriate ChangeDelta.
     *
     * @param p_key User's security key
     * @param p_issue The Issue object being editted
     * @param p_issueValue The value details for the Issue part of the issue
     *
     * @ejb.interface-method view-type="local"
     **/
    public void editIssue(KeySessionLocal p_keyLocal, 
                          IssueLocal p_issueLocal,
                          com.velocityme.valueobjects.IssueValue p_issueValue,
                          java.util.Collection p_responsibleContactableLocals,
                          java.util.Collection p_interestedContactableLocals) 
        throws PermissionDeniedException, InvalidKeyException {
        try {
            TaskLocal taskLocal = p_issueLocal.getTaskLocal();
            NodeLocal nodeLocal = taskLocal.getNodeLocal();
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, nodeLocal).contains(m_permissionIssueEdit)) {
                TaskValue taskValue = p_issueValue.getTaskValue();
                ChangeDeltaLocal changeDeltaLocal = ChangeDeltaUtil.getLocalHome().create(new Date(), ChangeDeltaTypeBean.MODERATE, p_keyLocal.getUserLocal(), nodeLocal.getDBObjectLocal());
                TaskSessionUtil.getLocalHome().create().editTaskHelper(p_keyLocal, taskLocal, taskValue, p_responsibleContactableLocals, p_interestedContactableLocals, changeDeltaLocal);
                p_issueLocal.setIssueValue(p_issueValue);
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(CreateException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * If the user has permission to access a issue then return the local object.
     *
     * @param p_key User's security key
     * @param p_issuePK The Issue object to lookup
     *
     * @ejb.interface-method view-type="local"
     **/
    public IssueLocal getIssueLocal(KeySessionLocal p_keyLocal, 
                                    IssuePK p_issuePK)
        throws PermissionDeniedException, InvalidKeyException {
        try {
            IssueLocal issueLocal = IssueUtil.getLocalHome().findByPrimaryKey(p_issuePK);
            NodeLocal nodeLocal = issueLocal.getTaskLocal().getNodeLocal();
            if (m_searchSessionLocal.getAssignedPermissionsLocal(p_keyLocal, nodeLocal).contains(m_permissionNodeView)) {
                return issueLocal;
            }
            else
                throw new PermissionDeniedException();
        }
        catch(NamingException e) {
            throw new EJBException(e);
        }
        catch(FinderException e) {
            throw new EJBException(e);
        }
    }
    
    /**
     * Create the Session Bean.
     * @throws CreateException 
     * @ejb:create-method view-type="remote"
     */
    public void ejbCreate() throws CreateException {
        try {
            m_searchSessionLocal = SearchSessionUtil.getLocalHome().create();
            m_permissionIssueCreate = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.ISSUE_CREATE)));
            m_permissionIssueEdit = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.ISSUE_EDIT)));
            m_permissionNodeView = PermissionUtil.getLocalHome().findByPrimaryKey(new PermissionPK(new Integer(PermissionBean.NODE_VIEW)));
        }
        catch(NamingException e) {
            throw new CreateException(e.getMessage());
        }
        catch(FinderException e) {
            throw new CreateException(e.getMessage());
        }
    }

    public void ejbActivate() throws java.rmi.RemoteException {
    }
    
    public void ejbPassivate() throws java.rmi.RemoteException {
    }
    
    public void ejbRemove() throws java.rmi.RemoteException {
    }
    
    public void setSessionContext(javax.ejb.SessionContext sessionContext) throws java.rmi.RemoteException {
        m_context = sessionContext;
    }
    
}
